function chartLinkReportNew(url,chartData,e,value){	
	var resultData = _.mapObject(chartData,function(cd,key){
		if (key != "reportId" && key != "chartId"){
			if (typeof value != "undefined" && isfunction(cd)){
				return encodeto64(String(value[cd.substring(cd.indexOf("(")+1,cd.indexOf(")"))]));	
			} 
			return encodeto64(String(cd));
		}
	});
    
    resultData.reportId=chartData.reportId;
    resultData.chartId=chartData.chartId;
    resultData.encode=true;
	
	if (typeof resultData != "undefined"){
		$.ajax({   
		    type : "POST",   
		    url : url,
		    data : resultData,
		    dataType: "json",
		    success: function upload(value){
		    	renderChartNew(value);
		    }
		});
	}
}
function chartLinkReport(url,chartData,e,option,obj){
	
	var a  = arguments;
	var resultData;
	if (a.length > 2){
		resultData = getUrl(chartData,e,option,obj);
	} else {
		resultData = _.mapObject(chartData,function(cd,key){
	    	return encodeto64(String(cd));
		});
	}
    
    resultData.reportId=chartData.reportId;
    resultData.chartId=chartData.chartId;
    resultData.encode=true;
	
	if (typeof resultData != "undefined"){
		$.ajax({   
		    type : "POST",   
		    url : url,
		    data : resultData,
		    dataType: "json",
		    success: function upload(value){
		    	renderChart(value);
		    }
		});
	}
}
function renderChartNew(value){
	var json = value;	
	
	_.each(value,function(items,i){
		var dom = $('[name='+i+']');
		if (dom.length > 1){
			_.each(items,function(item,j){
				var myChart = datavizCharts.initInstance(dom[j]);
				myChart.setOptionAndData(item.simpleJson,item.data.data,item.compId,item.themeId);
				myChart.render();
			});
		} else {
			var myChart = datavizCharts.initInstance(dom[0]);
			myChart.setOptionAndData(items.simpleJson,items.data.data,items.compId,items.themeId);
			myChart.render();
		}
	});
} 
function renderChart(value){
	var json = value;	
	
	_.each(value,function(items,i){
		//var dom = document.getElementsByName(i);
		var dom = $('[name='+i+']');
		if (dom.length > 1){
			_.each(items,function(item,j){
				var instance = getChartsInstanceByDom(item,dom[j]);
				instance.render();
			});
		} else {
			var instance = getChartsInstanceByDom(items,dom[0]);
			instance.render();
		}
	});
} 
function chartOpenReportNavNew(url,chartData,e,value){
	
//	var linkData = getUrl(chartData,e,option,obj);
	var linkData = _.mapObject(chartData,function(cd,key){
		if (key != "reportId" && key != "target" && isfunction(cd)){
			return encodeto64(String(value[cd.substring(cd.indexOf("(")+1,cd.indexOf(")"))]));	
		}
		return  encodeto64(String(cd));	
	});
    
	if (typeof linkData != "undefined"){
		var linkUrl = url + "&encode=true";
		_.each(linkData,function(ld,i){
			linkUrl += "&" + i + "=" + ld;
		});
	
		if (!!chartData.target){
			openReportNav(linkUrl,chartData.target);	
		} else {
			openReportNav(linkUrl);	
		}
		
	} else {
		openReportNav(url);	
	}
}
function chartOpenReportNav(url,chartData,e,option,obj){
	
	var linkData = getUrl(chartData,e,option,obj);
    
	if (typeof linkData != "undefined"){
		var linkUrl = url;
		_.each(linkData,function(ld,i){
			linkUrl += "&" + i + "=" + ld;
		});
	
		if (!!chartData.target){
			openReportNav(linkUrl,chartData.target);	
		} else {
			openReportNav(linkUrl);	
		}
		
	} else {
		openReportNav(url);	
	}
}

function getUrl(chartData,e,option,obj){
	var linkUrl;
	if (typeof e.seriesType == "undefined" && typeof e.componentType == "undefined"){		
		if(option.id=="gismap1"){
			var arr = []
			for (var i in obj) {
			    arr.push(obj[i]);
			    }
			linkUrl = _.mapObject(chartData,function(cd,key){
				if (isfunction(cd)){
					var i = cd.substring(12,13);
					if(arr.length >= parseInt(i)){
						return encodeto64(arr[i-1]);
					} else {
						return encodeto64(arr[arr.length - 1]);
					}
				} else {
					 return encodeto64(""+cd);
				}
			});
		}
		if (option.option.series[0].type == "rose"){
			var p1 = getResults(e,option.data.properties.legend.bind[0].name);
			var p2 = option.data.properties.value.bind[0].name;
			var p3 = option.data.properties.value.bind[1].name;
			var p4 = getResults(e,option.data.properties.value.bind[0].name);
			var p5 = getResults(e,option.data.properties.value.bind[1].name);
			linkUrl = configUrl(chartData,p1,p2,p3,p4,p5,p5,p5);
		} else if (option.option.series[0].type == "dot"){
			var p2 = getResults(e,option.data.properties.legend.bind[0].name);
			var p3 = getResults(e,option.data.properties.value.bind[0].name);
			var p1 = getResults(e,option.data.properties.xAxis.bind[0].name);
			var p4 = getResults(e,option.data.properties.yAxis.bind[0].name);
			linkUrl = configUrl(chartData,p1,p2,p3,p4,p4,p4,p4);
		} else if (option.option.series[0].type == "chord"){
			var p1 = getResults(e,option.data.properties.source.bind[0].name);
			var p2 = getResults(e,option.data.properties.target.bind[0].name);
			var p3 = getResults(e,p1 + "-" + option.data.properties.value.bind[0].name);
			var p4 = getResults(e,p2 + "-" + option.data.properties.value.bind[0].name);
			linkUrl = configUrl(chartData,p1,p2,p3,p4,p4,p4,p4);
		} else if (option.option.series[0].type == "sunburst"){
			linkUrl = configUrl(chartData,e.name,e.size,e.size,e.size,e.size,e.size,e.size);
		} else if (option.option.series[0].type == "stream"){
			var p1 = getResults(e,option.data.properties.xAxis.bind[0].name);
			var p2 = getResults(e,option.data.properties.legend.bind[0].name);
			var p3 = getResults(e,option.data.properties.value.bind[0].name);			
			linkUrl = configUrl(chartData,p1,p2,p3,p3,p3,p3,p3);
		} else if (option.option.series[0].type == "tagcloud"){
			var p1 = getResults(e,option.data.properties.text.bind[0].name);			
			var p2 = getResults(e,option.data.properties.value.bind[0].name);			
			linkUrl = configUrl(chartData,p1,p2,p2,p2,p2,p2,p2);
		}
	} else {
	
		if (typeof e.seriesType == "undefined"){
			return ;
		} else if (e.seriesType == "bar" || e.seriesType == "line" || e.seriesType == "map"){// 柱形图、折线图、地图、三个参数
			// 极坐标
			if (typeof option.polar != "undefined"){
				linkUrl = configUrl(chartData,e.value[0],e.value[1],e.value[1],e.value[1],e.value[1],e.value[1],e.value[1]);
			} else if (typeof option.xAxis !="undefined" && typeof option.yAxis !="undefined" && option.xAxis.type == "value" && option.yAxis.type == "value"){
				linkUrl = configUrl(chartData,e.value[0],e.seriesName,e.value[1],e.value[1],e.value[1],e.value[1],e.value[1]);
			} else {
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value,e.value,e.value,e.value,e.value);
			}
		} else if (e.seriesType == "pie" || e.seriesType == "funnel"){	// 饼图、漏斗图
			if (option.series.length > 1){ // 复合饼图
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value,e.value,e.value,e.value,e.value);
			} else {
				linkUrl = configUrl(chartData,e.name,e.value,e.value,e.value,e.value,e.value,e.value);
			}
		} else if (e.seriesType == "scatter"){
			if (typeof option.geo == "undefined"){
				var p1 = e.value[0];
				var p2 = e.value[1];
				var p3 = e.value[2];
				if (typeof option.polar != "undefined"){
					linkUrl = configUrl(chartData,option.angleAxis.data[p2],option.radiusAxis.data[p1],p3,p3,p3,p3,p3);
				} else if (option.series.length > 1){
					linkUrl = configUrl(chartData,p1,e.seriesName,p3,p2,p3,p3,p3);
				} else {
					linkUrl = configUrl(chartData,option.xAxis[0].data[p1],option.yAxis[0].data[p2],p3,p3,p3,p3,p3);
				}
			} else {
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value[2],e.value[2],e.value[2],e.value[2],e.value[2]);
			}	
		} else if (e.seriesType == "effectScatter"){
			if (option.series[0].type == "lines"){//航线图
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value[2],e.value[2],e.value[2],e.value[2],e.value[2]);	
			} else if (option.series[0].type == "effectScatter"){
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value[2],e.value[2],e.value[2],e.value[2],e.value[2]);
			}//其他
		} else if (e.seriesType == "graph"){
			if (e.dataType == "node"){
				linkUrl = configUrl(chartData,e.name,e.data.category,e.value,e.value,e.value,e.value,e.value);	
			} else {
				return ;
			}
		} else if (e.seriesType == "treemap" || e.seriesType == "gauge"){
			linkUrl = configUrl(chartData,e.name,e.value,e.value,e.value,e.value,e.value,e.value);			
		} else if (e.seriesType == "sankey"){
			if (e.dataType == "edge"){
				linkUrl = configUrl(chartData,e.data.source,e.data.target,e.data.value,e.value,e.value,e.value,e.value);
			} else {
				return ;
			}			
		} else if (e.seriesType == "boxplot"){//箱线图
			linkUrl = configUrl(chartData,e.name,e.seriesName,e.value[0],e.value[1],e.value[2],e.value[3],e.value[4]);
		} else if (e.seriesType == "candlestick"){//K线图
			if (e.componentSubType == "line"){//点击折线url
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value(),e.value(),e.value(),e.value(),e.value());
			} else {//点击烛台url
				linkUrl = configUrl(chartData,e.name,e.seriesName,e.value[0],e.value[1],e.value[2],e.value[3],e.value[3]);	
			}
		} else if (e.seriesType == "heatmap"){//热力图、热力地图
			if (typeof option.geo == "undefined"){//热力图
				var p1 = e.value[0];
				var p2 = e.value[1];
				var p3 = e.value[2];
				linkUrl = configUrl(chartData,option.xAxis[0].data[p1],option.yAxis[0].data[p2],p3,p3,p3,p3,p3);
			} else {//热力地图
				return;	
			}
		} else if (e.seriesType == "radar"){//雷达图
			linkUrl = configUrlforRadar(chartData,e.name,e,option);
		} 
	}
	return linkUrl;
}

function getResults(e,p){
	var result = "";
	_.each(e,function(value,key){
		if (key == p){
			result = value;
			return value;
		}
	});
	
	return result;
}
function configUrl(chartData,parma1,parma2,parma3,parma4,parma5,parma6,parma7){	 
	  var resultData = _.mapObject(chartData,function(cd,key){
		if (cd == "axis()" || cd == "xAxis()" || cd == "source()" || cd == "angleAxis()"){
			return encodeto64(String(parma1));	
		} else if (cd == "series()" || cd == "value(1)" || cd == "target()" || cd == "radiusAxis()"){
			return encodeto64(String(parma2));
		} else if (cd == "value()"  || cd == "lower()" || cd == "value(2)" || cd == "open()"){
			return encodeto64(String(parma3));
		} else if (cd == "Q3()" || cd == "close()" || cd == "yAxis()"){
			return encodeto64(String(parma4));
		} else if (cd == "median()" || cd == "lowest()"){
			return encodeto64(String(parma5));
		} else if (cd == "Q1()" || cd == "highest()"){
			return encodeto64(String(parma6));
		} else if (cd == "upper()"){
			return encodeto64(String(parma7));
		} else if (isfunction(cd)){
			var i = cd.substring(12,13);
			var result = ""+parma1;
			switch(i) {
	            case "1":
	                result = parma1;
	                break;
	            case "2":
	            	result = parma2;
	                break;
	            case "3":
	            	result = parma3;
	                break;
	            case "4":
	            	result = parma4;
	                break;
	            case "5":
	            	result = parma5;
	                break;
	            case "6":
	            	result = parma6;
	                break;
	            case "7":
	            	result = parma7;
	                break;
	        }
			return encodeto64(""+result);
			
		} else {
			return encodeto64(""+cd);
		}
	});
	
	return resultData;
}

function configUrlforRadar(chartData,parma1,e,option){
	_.each(chartData,function(cd,i){
		if (cd.indexOf("(") > -1 && cd.indexOf(")") > -1){
			if (cd == "series()"){
				return encodeto64(parma1);	
			} else if (cd.substring(0,5) == "value"){
				var indexStr = cd.substring(6,7);
				var index = parseInt(indexStr);
				if (index >= 0 && index < e.value.length){
					return encodeto64(String(e.value[index]));	
				} else {
					throw "图表公式出错";
				}
			} else if (cd.substring(0,4) == "axis"){
				var indexStr = cd.substring(5,6);
				var index = parseInt(indexStr);
				if (index >= 0 && index < option.radar.indicator.length){
					return encodeto64(String(option.radar.indicator[index].name));	
				} else {
					throw "图表公式出错";
				}
			} else if (isfunction(cd)){
				var indexStr = cd.substring(12,13);
				var index = parseInt(indexStr);
				if (index > 0 && index <= option.radar.indicator.length){
					return encodeto64(String(option.radar.indicator[index-1].name));	
				} else {
					throw "图表公式出错";
				}
				return encodeto64(result);
			}
		} else {
			return encodeto64(String(cd));
		}
	});
}

function isfunction(s) 
{ 
	var patrn=/^chartparams/; 
	if (!patrn.exec(s)){
		return false; 
	}
	return true;
}